{% extends '../_manage.html' %}

{% block title %} {{ _('All Replies') }} {% endblock %}

{% block head %}
<script>
$(function() {
    getJSON('/api/replies', {
        page: {{ pageIndex }}
    }, function (err, data) {
        if (err) {
            return fatal(err);
        }
        initVM(data.replies, data.page)
    });
});

function initVM(replies, page) {
    var vm = new Vue({
        el: '#vm',
        data: {
            replies: replies,
            page: page
        },
        methods: {
            deleteReply: function (r) {
                if (confirm('Reply will be deleted. Continue?')) {
                    postJSON('/api/replies/' + r.id + '/delete', function(err, result) {
                        if (err) {
                            return error(err);
                        }
                        refresh();
                    });
                }
            }
        }
    });
    $('#loading').hide();
    $('#vm').show();
}
</script>
{% endblock %}

{% block main %}

    <div id="error" class="uk-width-1-1">
    </div>

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> Loading...
    </div>

    <div id="vm" class="uk-width-1-1">
        <ul class="uk-tab" data-uk-tab>
            <li><a href="#0" onclick="location.assign('board_list')">Boards</a></li>
            <li><a href="#0" onclick="location.assign('topic_list')">Topics</a></li>
            <li class="uk-active"><a href="#0">Replies</a></li>
        </ul>
        <div class="uk-margin">
            <a href="javascript:refresh()" class="uk-button"><i class="uk-icon-refresh"></i> {{ _('Refresh') }}</a>
        </div>

        <table class="uk-table uk-table-hover">
            <thead>
                <tr>
                    <th width="10%">{{ _('Image') }}</th>
                    <th width="20%">{{ _('User') }}</th>
                    <th width="50%">{{ _('Content') }}</th>
                    <th width="15%">Created</th>
                    <th width="5%"></th>
                </tr>
            </thead>
            <tbody>
                <tr v-repeat="obj: replies">
                    <td>
                        <img v-attr="src: obj.user.image_url" class="uk-comment-avatar uk-border-circle x-avatar" style="width:50px;height:50px;">
                    </td>
                    <td>
                        <span v-text="obj.user.name"></span>
                    </td>
                    <td>
                        <p><a target="_blank" v-attr="href: '/discuss/topic/' + obj.topic_id + '/find/' + obj.id">Topic</a></p>
                        <div v-if="!obj.deleted" v-html="obj.content"></div>
                        <div v-if="obj.deleted"><div class="uk-alert uk-alert-danger">{{ _('Deleted') }}</div></div>
                    </td>
                    <td>
                        <span v-text="obj.created_at.toDateTime()"></span>
                    </td>
                    <td>
                        <a v-if="!obj.deleted" v-on="click: deleteReply(obj)" title="Delete Reply" href="#0" class="x-btn"><i class="uk-icon-trash"></i></a>
                    </td>
                </tr>
                <tr v-if="page.totalItems===0">
                    <td colspan="6">No reply found.</td>
                </tr>
            </tbody>
        </table>
        <div v-component="pagination" v-with="page"></div>
    </div>

{% endblock%}
